class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n=nums.size();
        vector<int> f(n+1);
        auto g =f;
        f[0]=0;
        g[0]=0;
        int sum=0;
        int ret1 = -0x3f3f3f3f; 
        int ret2 = 0x3f3f3f3f;
        for(int i=1;i<n+1;i++)
        {
            f[i] = max(f[i-1]+nums[i-1],nums[i-1]);
            ret1 = max(f[i],ret1);
            g[i] = min(g[i-1]+nums[i-1],nums[i-1]);
            ret2 = min(g[i],ret2);
            sum+=nums[i-1];
        }
        return sum==ret2?ret1:  max(ret1,sum-ret2);
        
    }
};